package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EmProperties;
import com.ruoyi.system.domain.bo.EmPropertiesBo;
import com.ruoyi.system.domain.vo.EmPropertiesVo;
import com.ruoyi.system.domain.vo.HomeBarVo;
import com.ruoyi.system.domain.vo.HomeCountVo;
import com.ruoyi.system.mapper.EmPropertiesMapper;
import com.ruoyi.system.service.IEmPropertiesService;
import com.ruoyi.system.service.ISysDeptService;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

/**
 * 不动产参数Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-25
 */
@RequiredArgsConstructor
@Service
public class EmPropertiesServiceImpl extends ServiceImpl<EmPropertiesMapper,EmProperties> implements IEmPropertiesService {

    private final DictService dictService;
    private final ISysDeptService deptService;


    /**
     * 查询不动产参数
     */
    @Override
    public EmPropertiesVo queryById(Long emId){
        return baseMapper.selectVoById(emId);
    }

    /**
     * 查询不动产参数列表
     */
    @Override
    public TableDataInfo<EmPropertiesVo> queryPageList(EmPropertiesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmProperties> lqw = buildQueryWrapper(bo);
        //处理权限
        //子公司只能看到自己公司的
        Long deptId = LoginHelper.getDeptId();
        //集团可以看到全部的
        SysDept sysDept = deptService.selectDeptById(deptId);

        //管理员也可以看到全部的
        boolean admin = LoginHelper.isAdmin();
        if (admin) {
            //看到全部
        } else if (sysDept.getDeptName().contains("集团")) {
            //判断部门的名称
            //看到全部
        }else{
            //查看自己部门
            lqw.eq(EmProperties::getCreateDept,deptId);
        }
        Page<EmPropertiesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询不动产参数列表
     */
    @Override
    public List<EmPropertiesVo> queryList(EmPropertiesBo bo) {
        LambdaQueryWrapper<EmProperties> lqw = buildQueryWrapper(bo);

        //处理权限
        //子公司只能看到自己公司的
        Long deptId = LoginHelper.getDeptId();
        //集团可以看到全部的
        SysDept sysDept = deptService.selectDeptById(deptId);

        //管理员也可以看到全部的
        boolean admin = LoginHelper.isAdmin();
        if (admin) {
            //看到全部
        } else if (sysDept.getDeptName().contains("集团")) {
            //判断部门的名称
            //看到全部
        }else{
            //查看自己部门
            lqw.eq(EmProperties::getCreateDept,deptId);
        }
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmProperties> buildQueryWrapper(EmPropertiesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmProperties> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEmCode()), EmProperties::getEmCode, bo.getEmCode());
        lqw.like(StringUtils.isNotBlank(bo.getEmName()), EmProperties::getEmName, bo.getEmName());
        lqw.eq(StringUtils.isNotBlank(bo.getEmType()), EmProperties::getEmType, bo.getEmType());
        lqw.eq(StringUtils.isNotBlank(bo.getEmStatus()), EmProperties::getEmStatus, bo.getEmStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getEmCompany()), EmProperties::getEmCompany, bo.getEmCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getEmLocation()), EmProperties::getEmLocation, bo.getEmLocation());
        lqw.eq(bo.getEmArea() != null, EmProperties::getEmArea, bo.getEmArea());
        lqw.eq(StringUtils.isNotBlank(bo.getEmOwnershipS()), EmProperties::getEmOwnershipS, bo.getEmOwnershipS());
        lqw.like(StringUtils.isNotBlank(bo.getEmOwnershipName()), EmProperties::getEmOwnershipName, bo.getEmOwnershipName());
        lqw.eq(StringUtils.isNotBlank(bo.getEmOwnershipMix()), EmProperties::getEmOwnershipMix, bo.getEmOwnershipMix());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMortgageY()), EmProperties::getEmMortgageY, bo.getEmMortgageY());
        lqw.like(StringUtils.isNotBlank(bo.getEmMortgageName()), EmProperties::getEmMortgageName, bo.getEmMortgageName());
        lqw.eq(StringUtils.isNotBlank(bo.getEmCertificateN()), EmProperties::getEmCertificateN, bo.getEmCertificateN());
        lqw.eq(StringUtils.isNotBlank(bo.getEmCertificateF()), EmProperties::getEmCertificateF, bo.getEmCertificateF());
        lqw.eq(StringUtils.isNotBlank(bo.getEmCertificateT()), EmProperties::getEmCertificateT, bo.getEmCertificateT());
        lqw.eq(StringUtils.isNotBlank(bo.getEmLandnature()), EmProperties::getEmLandnature, bo.getEmLandnature());
        lqw.eq(bo.getEmHoldingtime() != null, EmProperties::getEmHoldingtime, bo.getEmHoldingtime());
        lqw.eq(StringUtils.isNotBlank(bo.getEmHoldingway()), EmProperties::getEmHoldingway, bo.getEmHoldingway());
        lqw.eq(bo.getEmTao() != null, EmProperties::getEmTao, bo.getEmTao());
        lqw.eq(StringUtils.isNotBlank(bo.getEmFinAccount()), EmProperties::getEmFinAccount, bo.getEmFinAccount());
        lqw.eq(bo.getEmFinBook() != null, EmProperties::getEmFinBook, bo.getEmFinBook());
        lqw.eq(bo.getEmFinObook() != null, EmProperties::getEmFinObook, bo.getEmFinObook());
        lqw.eq(StringUtils.isNotBlank(bo.getEmEval()), EmProperties::getEmEval, bo.getEmEval());
        lqw.eq(bo.getEmEvalPrice() != null, EmProperties::getEmEvalPrice, bo.getEmEvalPrice());
        lqw.eq(bo.getEmEvalTime() != null, EmProperties::getEmEvalTime, bo.getEmEvalTime());
        lqw.eq(StringUtils.isNotBlank(bo.getEmEvalAgency()), EmProperties::getEmEvalAgency, bo.getEmEvalAgency());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInPaper()), EmProperties::getEmInPaper, bo.getEmInPaper());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInBc1()), EmProperties::getEmInBc1, bo.getEmInBc1());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInBc2()), EmProperties::getEmInBc2, bo.getEmInBc2());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInBc3()), EmProperties::getEmInBc3, bo.getEmInBc3());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInLabel()), EmProperties::getEmInLabel, bo.getEmInLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getEmInFlocation()), EmProperties::getEmInFlocation, bo.getEmInFlocation());
        lqw.eq(bo.getEmMonthIdle() != null, EmProperties::getEmMonthIdle, bo.getEmMonthIdle());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMonthIdleR()), EmProperties::getEmMonthIdleR, bo.getEmMonthIdleR());
        lqw.eq(bo.getEmMonthUse() != null, EmProperties::getEmMonthUse, bo.getEmMonthUse());
        lqw.eq(StringUtils.isNotBlank(bo.getEmOwnWay()), EmProperties::getEmOwnWay, bo.getEmOwnWay());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsWay()), EmProperties::getEmMerchantsWay, bo.getEmMerchantsWay());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsPlm()), EmProperties::getEmMerchantsPlm, bo.getEmMerchantsPlm());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsUnitw()), EmProperties::getEmMerchantsUnitw, bo.getEmMerchantsUnitw());
        lqw.eq(bo.getEmMerchantsDj() != null, EmProperties::getEmMerchantsDj, bo.getEmMerchantsDj());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelPaper()), EmProperties::getEmMeexcelPaper, bo.getEmMeexcelPaper());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelBc1()), EmProperties::getEmMeexcelBc1, bo.getEmMeexcelBc1());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelBc2()), EmProperties::getEmMeexcelBc2, bo.getEmMeexcelBc2());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelBc3()), EmProperties::getEmMeexcelBc3, bo.getEmMeexcelBc3());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelLabel()), EmProperties::getEmMeexcelLabel, bo.getEmMeexcelLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMeexcelFlocation()), EmProperties::getEmMeexcelFlocation, bo.getEmMeexcelFlocation());
        lqw.eq(bo.getEmMerchantsNumb() != null, EmProperties::getEmMerchantsNumb, bo.getEmMerchantsNumb());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgreementNo()), EmProperties::getEmAgreementNo, bo.getEmAgreementNo());
        lqw.eq(bo.getEmAgreementPrice() != null, EmProperties::getEmAgreementPrice, bo.getEmAgreementPrice());
        lqw.eq(bo.getEmMerchantsUnitp() != null, EmProperties::getEmMerchantsUnitp, bo.getEmMerchantsUnitp());
        lqw.eq(bo.getEmMerchantsTotal() != null, EmProperties::getEmMerchantsTotal, bo.getEmMerchantsTotal());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsObject()), EmProperties::getEmMerchantsObject, bo.getEmMerchantsObject());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsReal()), EmProperties::getEmMerchantsReal, bo.getEmMerchantsReal());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsYn()), EmProperties::getEmMerchantsYn, bo.getEmMerchantsYn());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsHistory()), EmProperties::getEmMerchantsHistory, bo.getEmMerchantsHistory());
        lqw.eq(bo.getEmMerchantsYears() != null, EmProperties::getEmMerchantsYears, bo.getEmMerchantsYears());
        lqw.eq(bo.getEmMerchantsBegin() != null, EmProperties::getEmMerchantsBegin, bo.getEmMerchantsBegin());
        lqw.eq(bo.getEmMerchantsEnd() != null, EmProperties::getEmMerchantsEnd, bo.getEmMerchantsEnd());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsCollectionway()), EmProperties::getEmMerchantsCollectionway, bo.getEmMerchantsCollectionway());
        lqw.eq(StringUtils.isNotBlank(bo.getEmMerchantsPayway()), EmProperties::getEmMerchantsPayway, bo.getEmMerchantsPayway());
        lqw.eq(bo.getEmDepossit() != null, EmProperties::getEmDepossit, bo.getEmDepossit());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelPaper()), EmProperties::getEmAgexcelPaper, bo.getEmAgexcelPaper());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelBc1()), EmProperties::getEmAgexcelBc1, bo.getEmAgexcelBc1());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelBc2()), EmProperties::getEmAgexcelBc2, bo.getEmAgexcelBc2());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelBc3()), EmProperties::getEmAgexcelBc3, bo.getEmAgexcelBc3());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelLabel()), EmProperties::getEmAgexcelLabel, bo.getEmAgexcelLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getEmAgexcelFlocation()), EmProperties::getEmAgexcelFlocation, bo.getEmAgexcelFlocation());
        lqw.eq(bo.getEmOweTotal() != null, EmProperties::getEmOweTotal, bo.getEmOweTotal());
        lqw.eq(StringUtils.isNotBlank(bo.getEmLiquWay()), EmProperties::getEmLiquWay, bo.getEmLiquWay());
        lqw.eq(bo.getEmRentAble() != null, EmProperties::getEmRentAble, bo.getEmRentAble());
        lqw.eq(bo.getEmRentPaid() != null, EmProperties::getEmRentPaid, bo.getEmRentPaid());
        lqw.eq(bo.getEmLateStart() != null, EmProperties::getEmLateStart, bo.getEmLateStart());
        lqw.eq(bo.getEmLateAllday() != null, EmProperties::getEmLateAllday, bo.getEmLateAllday());
        lqw.eq(bo.getEmLateMoney() != null, EmProperties::getEmLateMoney, bo.getEmLateMoney());
        lqw.eq(bo.getEmLateForpay() != null, EmProperties::getEmLateForpay, bo.getEmLateForpay());
        lqw.eq(bo.getEmOweLong() != null, EmProperties::getEmOweLong, bo.getEmOweLong());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelPaper()), EmProperties::getEmRenexcelPaper, bo.getEmRenexcelPaper());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelBc1()), EmProperties::getEmRenexcelBc1, bo.getEmRenexcelBc1());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelBc2()), EmProperties::getEmRenexcelBc2, bo.getEmRenexcelBc2());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelBc3()), EmProperties::getEmRenexcelBc3, bo.getEmRenexcelBc3());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelLabel()), EmProperties::getEmRenexcelLabel, bo.getEmRenexcelLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getEmRenexcelFlocation()), EmProperties::getEmRenexcelFlocation, bo.getEmRenexcelFlocation());
        lqw.eq(bo.getFormDataType() != null, EmProperties::getFormDataType, bo.getFormDataType());
        return lqw;
    }

    /**
     * 新增不动产参数
     */
    @Override
    public Boolean insertByBo(EmPropertiesBo bo) {
        EmProperties add = BeanUtil.toBean(bo, EmProperties.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setEmId(add.getEmId());
        }
        return flag;
    }

    /**
     * 修改不动产参数
     */
    @Override
    public Boolean updateByBo(EmPropertiesBo bo) {
        EmProperties update = BeanUtil.toBean(bo, EmProperties.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmProperties entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除不动产参数
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public HomeCountVo getCount() {
        return baseMapper.getCount();
    }

    @Override
    public List<HomeBarVo> getStatusBar() {

        List<HomeBarVo> data = baseMapper.getStatusBar();
        for (HomeBarVo datum : data) {
            String iemStatus = dictService.getDictLabel("iem_status", datum.getName());
            datum.setName(iemStatus);
        }
        return data;
    }

    @Override
    public List<HomeBarVo> getStatusBar2() {
        List<HomeBarVo> data = baseMapper.getStatusBar2();
        for (HomeBarVo datum : data) {
            String iemStatus = dictService.getDictLabel("iem_status", datum.getName());
            datum.setName(iemStatus);
        }
        return data;
    }

    @Override
    public List<HomeBarVo> getStatusBar3() {
        List<HomeBarVo> data = baseMapper.getStatusBar3();
        for (HomeBarVo datum : data) {
            String iemStatus = dictService.getDictLabel("iem_status", datum.getName());
            datum.setName(iemStatus);
        }
        return data;
    }

    /**
     * 租户数量变化图（柱状图）
     *
     * @return {@link List }<{@link HomeBarVo }>
     */
    @Override
    public List<Integer> getChange() {
        //遍历12个月
//遍历12个月
        LocalDate now = LocalDate.now();
        ArrayList<Integer> integers = Lists.newArrayList();

        int year = now.getYear();
        for (int i = 1; i <= 12; i++) {

            String format = String.format("%02d", i);
            String date = year + "-" + format;
            int count = baseMapper.getZuHuCount(date);
            integers.add(count);
        }

        return integers;
    }

    /**
     * 资产数量变化图（柱状图）
     *
     * @return {@link List }<{@link HomeBarVo }>
     */
    @Override
    public List<Integer> getChange2() {
        //遍历12个月
        LocalDate now = LocalDate.now();
        ArrayList<Integer> integers = Lists.newArrayList();

        int year = now.getYear();
        for (int i = 1; i <= 12; i++) {

            String format = String.format("%02d", i);
            String date = year + "-" + format;
            int count = baseMapper.getZiChanCount(date);
            integers.add(count);
        }

        return integers;
    }

    /**
     * @return {@link List }<{@link List }<{@link Double }>>
     */
    @Override
    public List<List<BigDecimal>> getReceivable() {
        //获取每个月的应收情况
        ArrayList<List<BigDecimal>> lists = Lists.newArrayList();
        ArrayList<BigDecimal> should = Lists.newArrayList();
        lists.add(should);
        ArrayList<BigDecimal> has = Lists.newArrayList();
        lists.add(has);
        //遍历今年的12个月
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        for (int i = 1; i <= 12; i++) {

            String format = String.format("%02d", i);
            String date = year + "-" + format;
            BigDecimal shouldItem = baseMapper.getShouldReceivable(date);
            BigDecimal hasItem = baseMapper.getHasReceivable(date);
            should.add(shouldItem);
            has.add(hasItem);

        }
        return lists;
    }
}
